package com.example.studentsystem.db;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

import com.example.studentsystem.R;

public class StudentCursorAdapter extends CursorAdapter {

    public StudentCursorAdapter(Context context, Cursor c, int i) {
        super(context, c, 0);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.student_list_item, parent, false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        try {
            int studentIdIndex = cursor.getColumnIndex("student_id");
            @SuppressLint("Range")
            String studentName = cursor.getString(cursor.getColumnIndex("student_name"));
            @SuppressLint("Range")
            String studentCode = cursor.getString(cursor.getColumnIndex("student_code"));
            @SuppressLint("Range")
            String username = cursor.getString(cursor.getColumnIndex("username"));
            @SuppressLint("Range")
            String className = cursor.getString(cursor.getColumnIndex("class_name"));

            TextView tvStudentId = view.findViewById(R.id.student_id);
            TextView tvStudentName = view.findViewById(R.id.student_name);
            TextView tvStudentCode = view.findViewById(R.id.student_code);
            TextView tvUsername = view.findViewById(R.id.username);
            TextView tvClassName = view.findViewById(R.id.class_name);

            tvStudentId.setText(String.valueOf(cursor.getInt(studentIdIndex)));
            tvStudentName.setText(studentName);
            tvStudentCode.setText(studentCode);
            tvUsername.setText(username);
            tvClassName.setText(className);
        } catch (IllegalArgumentException e) {
            // Handle the case where the column index is not found
            Log.e("StudentCursorAdapter", "Column index not found", e);
        }
    }
}